package drds.server.net.packet;

/**
 * 
 * <pre>
 * 
 * COM_STMT_SEND_LONG_DATA sends the data for a column. Repeating to send it, appends the data to the parameter.
 * No response is sent back to the client.
 * 
 * COM_STMT_SEND_LONG_DATA:
 * COM_STMT_SEND_LONG_DATA
 * direction: client -> server
 * response: none
 * 
 * payload:
 *   1              [18] COM_STMT_SEND_LONG_DATA
 *   4              statement-id
 *   2              param-id
 *   n              data
 * 
 * </pre>
 * 
 * @see https://dev.mysql.com/doc/internals/en/com-stmt-send-long-data.html
 * @author 曹正辉<330937205@qq.com>
 * 
 */
public class LongDataPacket extends Packet {

	private static final byte PACKET_FALG = (byte) 24;
	private long statementId;
	private long parameterId;
	private byte[] longData = new byte[0];

	public void read(byte[] bytes) {
		BytesHolder bytesHolder = new BytesHolder(bytes);
		packetLength = bytesHolder.readInt3();
		packetId = bytesHolder.read();
		byte code = bytesHolder.read();
		assert code == PACKET_FALG;
		statementId = bytesHolder.readLong4();
		parameterId = bytesHolder.readInt2();
		this.longData = bytesHolder.readBytes(packetLength - (1 + 4 + 2));
	}

	@Override
	public int computePacketSize() {
		return 1 + 4 + 2 + this.longData.length;
	}

	public long getStatementId() {
		return statementId;
	}

	public long getParameterId() {
		return parameterId;
	}

	public byte[] getLongData() {
		return longData;
	}

}
